import { app, BrowserWindow, nativeTheme, ipcMain } from 'electron'
import path from 'path'
// import os from 'os'
import ws通信 from './ws通信'
import ws from 'ws'

import windowStateKeeper from 'electron-window-state'

// needed in case process is undefined under Linux
const platform = process.platform || os.platform()

try {
  if (platform === 'win32' && nativeTheme.shouldUseDarkColors === true) {
    require('fs').unlinkSync(path.join(app.getPath('userData'), 'DevTools Extensions'))
  }
} catch (_) { }

let mainWindow

function createWindow() {
  /**
   * Initial window options
   */
  let winstate = windowStateKeeper({
    defaultWidth: 300, defaultHeight: 600
  })
  mainWindow = new BrowserWindow({
    icon: path.resolve(__dirname, 'icons/icon.png'), // tray icon
    width: winstate.width,
    height: winstate.height,
    x: winstate.x,
    y: winstate.y,
    minHeight: 200,
    minWidth: 100,
    menuBarVisible: false,
    useContentSize: true,
    autoHideMenuBar: true,
    // alwaysOnTop: true,
    webPreferences: {
      contextIsolation: true,
      // //渲染进程是否启用remote模块
      // More info: https://v2.quasar.dev/quasar-cli-vite/developing-electron-apps/electron-preload-script
      // preload: path.resolve(__dirname, process.env.QUASAR_ELECTRON_PRELOAD),

      // nodeIntegration: true,//nodeIntegration设为false页面就不能使用nodejs和Electron APIs

      //   enableRemoteModule: true,//在渲染进程中使用主进程中的模块方法时，可以使用Electron Remote解决在渲染和主进程间的通讯,这么看 remote 模块可以说是非常好用啦，渲染进程就不用显式通过 ipcRenderer / ipcMain 与主进程通信。除此之外，可以通过 remote.getGlobal 获取主进程中 的全局变量, 通过 remote.process 拿到主进程的 process 对象信息
      // preload: path.join(__dirname, 'preload.js')
    },
  })

  mainWindow.setAlwaysOnTop(true)
  // mainWindow.setAlwaysOnTop(true, "floating", 1);

  mainWindow.loadURL(process.env.APP_URL)
  // console.log(process.env.APP_URL);
  // mainWindow.loadFile(process.env.APP_URL)

  // mainWindow.setMenu(null);// 永久隐藏菜单栏

  if (process.env.DEBUGGING) {
    // if on DEV or Production with debug enabled
    mainWindow.webContents.openDevTools()
  } else {
    // we're on production; no access to devtools pls
    // mainWindow.webContents.on('devtools-opened', () => {
    //   mainWindow.webContents.closeDevTools()
    // })
  }

  winstate.manage(mainWindow)

  mainWindow.on('closed', () => {
    mainWindow = null
  })

  ws通信.constructor(mainWindow);
  // new ws通信();

}

app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
  if (platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

// new 通信();
// ws通信.constructor(mainWindow);

ipcMain.on('setTitle', (event) => {
  const webContents = event.sender
  const win = BrowserWindow.fromWebContents(webContents)
  win.setTitle("amtfkk")
})

async function 检查su连接() {
  return "等下再检查" // 返回给渲染进程
}

ipcMain.handle('检查su连接', 检查su连接)


// const WebSocketServer = ws.Server;
// aws = new WebSocketServer({ port: 12111 });
// aws.on('connection', (ws) => {
//   // 有客户端连接时, 打印一条日志
//   console.log('ws client connected');
//   // 并且创建'message'监听
//   ws.on('message', (message) => {
//     // 直接将消息打印出来
//     //     console.log(message);
//     // console.log('客户端消息：' + message);
//     kk = JSON.parse(message)
//     console.log(kk);
//     if (kk['amtf'] == "su已经连接") {
//       mainWindow.webContents.send('更新su连接状态', "su已连接")
//     } else {

//     }
//   });
// })
